package com.hitqz.robot.biz.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hitqz.robot.api.business.dto.PatrolItemStandardSyncDto;
import com.hitqz.robot.api.business.dto.RobotSendClientMsgDto;
import com.hitqz.robot.api.business.entity.SentinelModeRecordEntity;
import com.hitqz.robot.api.dispatch.protocol.ProtocolEnum;
import com.hitqz.robot.api.dispatch.protocol.dto.RobotAsyncCmdDto;
import com.hitqz.robot.biz.service.SentinelModeRecordService;
import com.hitqz.robot.common.core.util.R;
import com.hitqz.robot.common.core.util.RedisUtils;
import com.hitqz.robot.common.security.service.RobotUser;
import com.hitqz.robot.common.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 哨兵模式记录控制器
 * 
 * @author 
 * @since 2025-08-15
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sentinelModeRecord")
@Tag(name="哨兵模式记录管理")
public class SentinelModeRecordController {

    private final SentinelModeRecordService sentinelModeRecordService;

    public record RequestRecordParams(int size,int current,String robotName){}

    /**
     * 分页查询哨兵模式记录
     */
    @PostMapping("/page")
    @Operation(summary = "分页查询")
    public R<IPage<SentinelModeRecordEntity>> page(@RequestBody RequestRecordParams params ) {
        LambdaQueryWrapper<SentinelModeRecordEntity> wrapper = Wrappers.lambdaQuery();
        Page<SentinelModeRecordEntity> page = new Page<>(params.current(), params.size());
        wrapper.like(StrUtil.isNotBlank(params.robotName),SentinelModeRecordEntity::getRobotName, params.robotName);
        return R.ok(sentinelModeRecordService.page(page, wrapper));
    }

    /**
     * 哨兵模式开关
     * @param clientId robotSn
     * @param  enable  false 关闭 true 开启
     */
    @GetMapping("/switch/{clientId}")
    @PreAuthorize("@pms.hasPermission('set_sentinel_mode')")
    @Operation(summary = "哨兵模式开关")
    public R<Boolean> switchSentinelMode(@PathVariable String clientId,@RequestParam(value = "enable") boolean enable) {
        RedisUtils.set("sentinelMode:" + clientId, enable);
        RobotSendClientMsgDto msgDto= new RobotSendClientMsgDto();
        msgDto.setCmd(ProtocolEnum.ROBOT_SYNC_CMD.getCmd());
        msgDto.setClientId(clientId);
        RobotAsyncCmdDto robotAsyncCmdDto = new RobotAsyncCmdDto();
        if ( enable) {
            robotAsyncCmdDto.setCmd(105);
        }else {
            robotAsyncCmdDto.setCmd(106);
        }
        msgDto.setData(JSONObject.toJSONString(robotAsyncCmdDto).getBytes(StandardCharsets.UTF_8));
        SpringUtil.getApplicationContext().publishEvent(msgDto);
        return R.ok();
    }

    /**
     * 获取哨兵模式开关状态
     * @param clientId robotSn
     */
    @GetMapping("/switch/{clientId}/status")
    @Operation(summary = "获取哨兵模式开关状态")
    public R<Boolean> getSentinelModeSwitch(@PathVariable String clientId) {
        Boolean enabled = RedisUtils.get("sentinelMode:" + clientId);
        return R.ok(enabled != null && enabled);
    }



}